package com.zhuiyun.project.api.plan.fitnessSeries.service.impl;

import com.zhuiyun.project.api.plan.fitnessPlan.entity.FitnessPlan;
import com.zhuiyun.project.api.plan.fitnessPlan.mapper.FitnessPlanMapper;
import com.zhuiyun.project.api.plan.fitnessPlan.model.FitnessPlanModel;
import com.zhuiyun.project.api.plan.fitnessPlan.service.FitnessPlanService;
import com.zhuiyun.project.api.plan.fitnessPlanAction.entity.FitnessPlanAction;
import com.zhuiyun.project.api.plan.fitnessPlanAction.mapper.FitnessPlanActionMapper;
import com.zhuiyun.project.api.plan.fitnessPlanAction.model.FitnessPlanActionModel;
import com.zhuiyun.project.api.plan.fitnessPlanActionCount.entity.FitnessPlanActionCount;
import com.zhuiyun.project.api.plan.fitnessPlanActionCount.mapper.FitnessPlanActionCountMapper;
import com.zhuiyun.project.api.plan.fitnessPlanActionCount.model.FitnessPlanActionCountModel;
import com.zhuiyun.project.api.plan.fitnessPlanActionGroup.entity.FitnessPlanActionGroup;
import com.zhuiyun.project.api.plan.fitnessPlanActionGroup.mapper.FitnessPlanActionGroupMapper;
import com.zhuiyun.project.api.plan.fitnessPlanActionGroup.model.FitnessPlanActionGroupModel;
import com.zhuiyun.project.api.plan.fitnessPlanActionGroup.service.FitnessPlanActionGroupService;
import com.zhuiyun.project.api.plan.fitnessSeries.entity.FitnessPurpose;
import com.zhuiyun.project.api.plan.fitnessSeries.entity.FitnessSeries;
import com.zhuiyun.project.api.plan.fitnessSeries.mapper.FitnessSeriesMapper;
import com.zhuiyun.project.api.plan.fitnessSeries.model.FitnessPurposeModel;
import com.zhuiyun.project.api.plan.fitnessSeries.model.FitnessSeriesModel;
import com.zhuiyun.project.api.plan.fitnessSeries.service.FitnessSeriesService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName FitnessActionServiceImpl
 * @Description TODO
 * @Author ys
 * @Date 2023/11/2 9:01
 **/
@Service
public class FitnessSeriesServiceImpl implements FitnessSeriesService {
    @Resource
    private FitnessSeriesMapper fitnessSeriesMapper;
    @Resource
    private FitnessPlanMapper fitnessPlanMapper;
    @Resource
    private FitnessPlanActionMapper fitnessPlanActionMapper;
    @Autowired
    private FitnessPlanActionGroupService fitnessPlanActionGroupService;
    @Resource
    private FitnessPlanActionCountMapper fitnessPlanActionCountMapper;
    /**
     * @Author 杨珅
     * @Description todo 健身系列分页查询
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public PageResult selectAll(PageRequest pageRequest, String seriesName,String venueUniqueIdentifier) {
        MybatisPageHelper.startPage(pageRequest);// 使用MybatisPageHelper实现分页功能，设置分页信息
        List<FitnessSeriesModel> PlanList = fitnessSeriesMapper.selectAll(pageRequest, seriesName,venueUniqueIdentifier);// 调用ArticleReviewMapper的getArticle方法查询文章列表\
        return MybatisPageHelper.getPageResult(PlanList);
    }
    /**
     * @Author 杨珅
     * @Description todo 根据教练id查询系列
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public PageResult selectCoachAll(PageRequest pageRequest, String seriesName, String venueUniqueIdentifier, Integer coachId) {
        MybatisPageHelper.startPage(pageRequest);// 使用MybatisPageHelper实现分页功能，设置分页信息
        List<FitnessSeriesModel> PlanList = fitnessSeriesMapper.selectCoachAll(pageRequest, seriesName,venueUniqueIdentifier,coachId);
        return MybatisPageHelper.getPageResult(PlanList);
    }
    /**
     * @Author 杨珅
     * @Description todo 根据教练id查询系列
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public CommonResult selectCoachAlls( String seriesName, String venueUniqueIdentifier, Integer coachId) {
        List<FitnessSeriesModel> PlanList = fitnessSeriesMapper.selectCoachAllS(seriesName,venueUniqueIdentifier,coachId);
        return CommonResult.ok(PlanList);
    }
//    /**
//     * @Author 杨珅
//     * @Description todo 根据学院id查询计划
//     * @Date 2023/11/13 9:01
//     * @Return  * @param null
//     */
//    @Override
//    public CommonResult selectPlanById(Integer memberId) {
//        List<FitnessPlanModel> fitnessPlanModels = fitnessPlanMapper.selectPlanById(memberId);
//        if(fitnessPlanModels.isEmpty()){
//            return CommonResult.ok(null);
//        }else{
//            return CommonResult.ok(fitnessPlanMapper.selectPlanById(memberId));
//        }
//    }
//
    /**
     * @Author 杨珅
     * @Description todo 健身系列新增
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    @Transactional
    public CommonResult insertFitnessSeries(FitnessSeriesModel fitnessSeriesModel){
        // 设置创建时间和更新时间为当前时间
        fitnessSeriesModel.setCreateTime(new Date());
        fitnessSeriesModel.setUpdateTime(new Date());

        // 插入 FitnessSeriesModel
        int i = fitnessSeriesMapper.insertFitnessSeries(fitnessSeriesModel);

        // 判断插入是否成功
        if (i >= 1) {
            List<FitnessPlanModel> planArray = fitnessSeriesModel.getPlanArray();
            for (FitnessPlanModel plan : planArray) {
                // 设置 FitnessPlanModel 的 fitnessSeriesId 和 enableStatus，并设置创建时间和更新时间
                plan.setFitnessSeriesId(fitnessSeriesModel.getId());
                plan.setEnableStatus("0");
                plan.setCreateTime(new Date());
                plan.setUpdateTime(new Date());
                // 插入 FitnessPlanModel
                fitnessPlanMapper.insertFitnessPlan(plan);
                Integer id = plan.getId();
                List<FitnessPlanActionModel> planActionArray = plan.getPlanActionArray();
                for (FitnessPlanActionModel planAction : planActionArray) {
                    // 设置 FitnessPlanActionModel 的 createTime、updateTime 和 fitnessPlanId
                    planAction.setCreateTime(new Date());
                    planAction.setUpdateTime(new Date());
                    planAction.setFitnessPlanId(id);
                    // 插入 FitnessPlanActionModel
                    fitnessPlanActionMapper.insertFitnessPlanAction(planAction);
                    // 获取 FitnessPlanActionModel 的 group，循环遍历插入到数据库
                    List<FitnessPlanActionCount> group = planAction.getGroup();
                    for (FitnessPlanActionCount fitnessPlanActionCount : group) {
                        // 设置 fitnessPlanActionCount 的 id 为 null，因为插入时会自动生成
                        fitnessPlanActionCount.setId(null);
                        fitnessPlanActionCount.setPlanActionId(planAction.getId());
                        fitnessPlanActionCount.setVenueUniqueIdentifier(planAction.getVenueUniqueIdentifier());
                        // 插入 FitnessPlanActionCount
                        fitnessPlanActionCountMapper.insertFitnessPlanActionCount(fitnessPlanActionCount);
                    }
                }
            }
            // 插入成功，返回包含 fitnessSeriesModel.getId() 的 CommonResult 对象
            return CommonResult.ok(fitnessSeriesModel.getId());
        }
        else {
            // 插入失败，返回 SAVE_ERROR 的 CommonResult 对象
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }
    /**
     * @Author 杨珅
     * @Description todo 健身系列新增
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public CommonResult insertFitnessSeriesss(FitnessSeriesModel fitnessSeriesModel){
        // 设置创建时间和更新时间为当前时间
        fitnessSeriesModel.setCreateTime(new Date());
        fitnessSeriesModel.setUpdateTime(new Date());

        List<FitnessPlanModel> planArray = fitnessSeriesModel.getPlanArray();
        for (FitnessPlanModel plan : planArray) {
            // 设置 FitnessPlanModel 的 fitnessSeriesId 和 enableStatus，并设置创建时间和更新时间
            plan.setFitnessSeriesId(fitnessSeriesModel.getId());
            plan.setEnableStatus("0");
            plan.setCreateTime(new Date());
            plan.setUpdateTime(new Date());
            // 插入 FitnessPlanModel
            fitnessPlanMapper.insertFitnessPlan(plan);
            Integer id = plan.getId();
            List<FitnessPlanActionModel> planActionArray = plan.getPlanActionArray();
            for (FitnessPlanActionModel planAction : planActionArray) {
                // 设置 FitnessPlanActionModel 的 createTime、updateTime 和 fitnessPlanId
                planAction.setCreateTime(new Date());
                planAction.setUpdateTime(new Date());
                planAction.setFitnessPlanId(id);
                // 插入 FitnessPlanActionModel
                fitnessPlanActionMapper.insertFitnessPlanAction(planAction);
                // 获取 FitnessPlanActionModel 的 group，循环遍历插入到数据库
                List<FitnessPlanActionCount> group = planAction.getGroup();
                for (FitnessPlanActionCount fitnessPlanActionCount : group) {
                    // 设置 fitnessPlanActionCount 的 id 为 null，因为插入时会自动生成
                    fitnessPlanActionCount.setId(null);
                    fitnessPlanActionCount.setPlanActionId(planAction.getId());
                    // 插入 FitnessPlanActionCount
                    fitnessPlanActionCountMapper.insertFitnessPlanActionCount(fitnessPlanActionCount);
                }
            }
        }
        // 插入成功，返回包含 fitnessSeriesModel.getId() 的 CommonResult 对象
        return CommonResult.ok(fitnessSeriesModel.getId());
    }
//    /**
//     * @Author 杨珅
//     * @Description todo 健身计划修改
//     * @Date 2023/11/13 9:01
//     * @Return  * @param null
//     */
//    @Override
//    public CommonResult updateFitnessPlan(FitnessPlan fitnessPlan)throws CommonException {
//        if (ValidatorUtil.ValidationObject(fitnessPlan)){
//            int i = fitnessPlanMapper.updateFitnessPlan(fitnessPlan);
//            if (i >= 1) {
//                return CommonResult.ok(EmErrorCode.UPDATE_OK);
//            }else {
//                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
//            }
//        } else{
//            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
//        }
//    }
    /**
     * @Author 杨珅
     * @Description todo 健身计划逻辑删除
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public CommonResult deleteFitnessSeries(FitnessSeries fitnessSeries) {
        return CommonResult.ok(fitnessSeriesMapper.deleteFitnessSeries(fitnessSeries));
    }

    /**
    * @Author 田 
    * @Description 查询健身系列训练部位
    * @Date 2024/3/13 16:34
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param fitnessPurpose
    */
    @Override
    public CommonResult selectPurpose(FitnessPurpose fitnessPurpose) {
        return CommonResult.ok(fitnessSeriesMapper.selectPurpose(fitnessPurpose));
    }

    /**
    * @Author fei
    * @Description 添加健身系列目的和动作
    * @Date 2024/3/14 10:32
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param fitnessPurposeModel
    */
    @Override
    public CommonResult insertPurPoseAction(FitnessPurposeModel fitnessPurposeModel) {
        return CommonResult.ok(fitnessSeriesMapper.insertPurPoseAction(fitnessPurposeModel));
    }
}
